package com.ltu.timkor6.gamesite.admin.client;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.ltu.timkor6.gamesite.client.GameSite;
import com.ltu.timkor6.gamesite.shared.UserInfo;

public class Admin
{
	public static final String REGEX_EMAIL = "^[a-zA-Z][\\w\\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\\w\\.-]*[a-zA-Z0-9]\\.[a-zA-Z][a-zA-Z\\.]*[a-zA-Z]$";
	
	private GameSite gameSite = null;
	
	private CompositeUserInfo compositeUserInfo;
	private CompositeUserList compositeUserList;
	private AdminServiceAsync service = GWT.create(AdminService.class);
	
	private long currentId = 0;
	
	public Admin(GameSite gameSite)
	{
		this.gameSite = gameSite;
	}
	
	protected void checkUserInfo()
	{
		String password = compositeUserInfo.getPassword();
		String rePassword = compositeUserInfo.getRePassword();
		String email = compositeUserInfo.getEmail();
		boolean testUser = compositeUserInfo.getTestUser();
		boolean admin = compositeUserInfo.getAdmin();
		
		boolean error = false;
		
		if(password.compareTo(rePassword) != 0)
		{
			error = true;
			
			compositeUserInfo.addError("Passwords does not match.");
		}
		
		if(email.isEmpty())
		{
			error = true;
			
			compositeUserInfo.addError("Email cannot be empty.");
		}
		else if(!email.matches(REGEX_EMAIL))
		{
			error = true;
			
			compositeUserInfo.addError("Email is not valid.");
		}
		
		if(!error)
		{
			UserInfo userInfo = new UserInfo();
			userInfo.setId(this.currentId);
			userInfo.setIsAdmin(admin);
			userInfo.setIsTestUser(testUser);
			userInfo.setEmail(email);
			
			if(0 < password.length())
			{
				userInfo.setPassword(password);
			}
			
			setUserInfo(userInfo);
		}
	}
	
	public Composite showUserList()
	{
		this.compositeUserList = new CompositeUserList(this);
		
		getUserList();
		
		return this.compositeUserList;
	}
	
	public Composite showUserInfo(long userId)
	{
		this.compositeUserInfo = new CompositeUserInfo(this);
		
		getUserInfo(userId);
		
		return this.compositeUserInfo;
	}
	
	private void getUserList()
	{
		AsyncCallback<List<UserInfo>> callback = new AsyncCallback<List<UserInfo>>()
		{
			public void onFailure(Throwable caught)
			{
				Window.alert("Error: " + caught.getMessage());
			}
			
			public void onSuccess(List<UserInfo> list)
			{
				compositeUserList.updateComposite(list);
			}
		};
		
		service.getUserList(callback);
	}
	
	private void getUserInfo(long userId)
	{
		AsyncCallback<UserInfo> callback = new AsyncCallback<UserInfo>()
		{
			public void onFailure(Throwable caught)
			{
				Window.alert("Error: " + caught.getMessage());
			}
			
			public void onSuccess(UserInfo userInfo)
			{
				if(userInfo != null)
				{
					currentId = userInfo.getId();
					gameSite.setContent(compositeUserInfo);
					compositeUserInfo.updateComposite(userInfo);
				}
			}
		};
		
		service.getUserInfo(userId, callback);
	}
	
	private void setUserInfo(final UserInfo userInfo)
	{
		AsyncCallback<Boolean> callback = new AsyncCallback<Boolean>()
		{
			public void onFailure(Throwable caught)
			{
				Window.alert("Error: " + caught.getMessage());
			}
			
			public void onSuccess(Boolean success)
			{
				if(success)
				{
					compositeUserInfo.updateComposite(userInfo);
				}
			}
		};
		
		service.setUserInfo(userInfo, callback);
	}
}
